package com.ele.client.activity.auth;

import com.github.gwtbootstrap.client.ui.*;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class LoginViewImpl extends Composite implements LoginView {

    interface DefaultUiBinder extends UiBinder<Widget, LoginViewImpl> {}
    private static DefaultUiBinder uiBinder = GWT.create(DefaultUiBinder.class);

    private Presenter presenter;

    @UiField
    TextBox emailTextBox;
    @UiField
    PasswordTextBox passwordTextBox;
    @UiField
    Button signInButton;
    @UiField
    AlertBlock alertBlock;
    @UiField
    NavLink signUpButton;

    public LoginViewImpl() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @Override
    public void setEmail(String email) {
//        emailTextBox.setValue(email);
        emailTextBox.setValue("qq@qq.qq");
        passwordTextBox.setValue("1111");
    }

    @Override
    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void startLoading() {
        signInButton.state().loading();
    }

    @Override
    public void stopLoading() {
        signInButton.state().reset();
    }

    @Override
    public void showAlert() {
        alertBlock.setVisible(true);
    }

    @UiHandler("signInButton")
    void onSignInButtonClick(ClickEvent event) {
        presenter.login(emailTextBox.getValue(), passwordTextBox.getValue());
    }

    @UiHandler("signUpButton")
    void onSignUpButtonClick(ClickEvent event) {
        presenter.goToRegister(emailTextBox.getValue());
    }

    @UiHandler({"emailTextBox", "passwordTextBox"})
    void onKeyDown(KeyDownEvent event) {
        if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
            presenter.login(emailTextBox.getValue(), passwordTextBox.getValue());
        }
    }
}
